package com.geez.bbl.wordlist;

import com.geez.bbl.db.type.BblBook;
import com.geez.bbl.db.type.BblChapter;
import com.geez.bbl.db.type.Builder;
import com.geez.bbl.db.type.Verse;
import com.geez.bbl.util.FileUtils;
import com.geez.bbl.util.GeezUtil;
import com.geez.bbl.util.ProjectResources;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author biniam.gebremichael
 * @since 2/1/12
 */
public class WordList {

    Map<String, Integer> wl = new HashMap<String, Integer>();
    Map<String, Integer> wl2 = new HashMap<String, Integer>();
    Map<String, Integer> wl3 = new HashMap<String, Integer>();

    public WordList() {
    }

    public static void main(String[] args) throws Exception {
        List<BblBook> books = new Builder("ti").build();
        WordList wordList = new WordList();
        FileUtils.save(ProjectResources.getResourceFile("ti_wordlist.sql"),
                wordList.toSql("ti_wordlist", books));
    }


    public void process(List<BblBook> bblBooks) {

        for (BblBook bblBook : bblBooks) {
            for (BblChapter bblChapter : bblBook.getChapters().values()) {
                for (Verse verse : bblChapter.getVerses()) {
                    String[] words = removePunctuations(verse.getValue()).split("\\s+");
                    for (String word : words) {
                        getWord(wl,word);
                    }
                }
            }
        }
    }

    public void process3(List<BblBook> bblBooks) {
        for (BblBook bblBook : bblBooks) {
            for (BblChapter bblChapter : bblBook.getChapters().values()) {
                for (Verse verse : bblChapter.getVerses()) {
                    String[] words = removePunctuations(verse.getValue()).split("\\s+");
                    if (words.length > 3) {
                        for (int i = 2; i < words.length; i++) {
                            String word =words[i - 2] + " "+ words[i - 1] + " " + words[i];
                            getWord(wl3,word);
                        }
                    }
                }
            }
        }
    }


    public void process2(List<BblBook> bblBooks) {
        for (BblBook bblBook : bblBooks) {
            for (BblChapter bblChapter : bblBook.getChapters().values()) {
                for (Verse verse : bblChapter.getVerses()) {
                    String[] words = removePunctuations(verse.getValue()).split("\\s+");
                    if (words.length > 3) {
                        for (int i = 1; i < words.length; i++) {
                            String word = words[i - 1] + " " + words[i];
                            getWord(wl2,word);
                        }
                    }
                }
            }
        }
    }

    private void getWord(Map<String, Integer> wl,String word) {
        if (word.length() >= 2) {
            if (!wl.containsKey(word)) {
                wl.put(word, 1);
            } else {
                wl.put(word, wl.get(word) + 1);
            }
        }
    }

    public String removePunctuations(String s) {
        char word[] = s.toCharArray();
        StringBuilder b = new StringBuilder();
        for (char aWord : word) {
            int ord = (int) aWord;
            if (ord >= 4954) {
                b.append(' ');
            } else {
                b.append(aWord);
            }
        }
        return b.toString();
    }

    public String toSql(String tableName, List<BblBook> bblBooks) {
       process(bblBooks);
        StringBuilder sql = new StringBuilder();
        for (Map.Entry<String, Integer> entry : wl.entrySet()) {
            sql.append(toSql(tableName, entry.getKey(), entry.getValue(),1));
        }

//        process2(bblBooks);
//        for (Map.Entry<String, Integer> entry : wl2.entrySet()) {
//            sql.append(toSql(tableName, entry.getKey(), entry.getValue(),2));
//        }
//
//        process3(bblBooks);
//        for (Map.Entry<String, Integer> entry : wl3.entrySet()) {
//            sql.append(toSql(tableName, entry.getKey(), entry.getValue(),3));
//        }

        return sql.toString();
    }

    public String toSql(String tableName, String word, int occ, int size) {
        StringBuilder line = new StringBuilder();

        String wd = word.trim();
        String sera = GeezUtil.toSera(wd);
        line.append("INSERT INTO ")
                .append(tableName)
                .append(" (word,occurance,sera,novowel,size) VALUES ( ")
                .append("'")
                .append(wd)
                .append("'")
                .append(",")
                .append(occ)
                .append(",")
                .append("'")
                .append(sera)
                .append("'")
                .append(",")
                .append("'")
                .append(GeezUtil.removeVowel(sera))
                .append("'")
                .append(",")
                .append(size)
                .append(");\n");

        return line.toString();
    }
}
